using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Business;
using Entity;

public partial class EmployeePage_EquipmentManage : System.Web.UI.Page
{
    eEquipment equipment;
    DataTable dt_Type;
    protected void Page_Load(object sender, EventArgs e)
    {
        dt_Type = bType.getAllType();
        if (!IsPostBack)
        {
            DataTable dt = bEquipment.getAllEquipment(); ;
            gvEquipment.DataSource = dt;
            gvEquipment.DataBind();
        }
        btDelete.Attributes.Add("onclick", "return confirm('Are you sure you want to DELETE this equipment')");
    }

    protected void gvEmployee_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Detail")
        {
            btAdd.Text = "Update";
            int EqID = int.Parse(e.CommandArgument.ToString());
            equipment = bEquipment.getEquipmentByID(EqID);
            resetForm();
            MultiView1.ActiveViewIndex = 1;
        }
    }
    void resetForm()
    {
        if (btAdd.Text == "Update")
        {
            if (equipment != null)
            {
                txtID.Text = equipment.EquipmentID.ToString();
                txtName.Text = equipment.EquipmentName;
                txtDescription.Text = equipment.EquipmentDescription;
                txtAmount.Text = equipment.EquipmentAmount.ToString();
            }
        }
        else
        {
            txtID.Text = "";
            txtName.Text = "";
            txtDescription.Text = "";
            txtAmount.Text = "";
        }
        lbMesss.Text = "";
    }



    protected void btBack_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
    protected void btAdd_Click(object sender, EventArgs e)
    {
        if (btAdd.Text == "Update")
        {
            int EqID = int.Parse(txtID.Text);
            equipment = bEquipment.getEquipmentByID(EqID);
            equipment.EquipmentName = txtName.Text;
            equipment.EquipmentDescription = txtDescription.Text;
            equipment.EquipmentAmount = int.Parse(txtAmount.Text);
            bEquipment.updateEquipment(equipment);
            lbMesss.Text = "Has Updated";

            DataTable dt = bEquipment.getAllEquipment();
            gvEquipment.DataSource = dt;
            gvEquipment.DataBind();
        }
        else
        {
            equipment = new eEquipment();
            equipment.EquipmentName = txtName.Text;
            equipment.EquipmentDescription = txtDescription.Text;
            equipment.EquipmentAmount = int.Parse(txtAmount.Text);
            bEquipment.insertEquipment(equipment);
            lbMesss.Text = "Has Added";

            DataTable dt = bEquipment.getAllEquipment();
            gvEquipment.DataSource = dt;
            gvEquipment.DataBind();
            MultiView1.ActiveViewIndex = 0;

        }
    }

    protected void btReset_Click(object sender, EventArgs e)
    {
        if (btAdd.Text == "Update")
        {
            int EqID = int.Parse(txtID.Text);
            equipment = bEquipment.getEquipmentByID(EqID);
        }
        resetForm();
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        btAdd.Text = "Add New";
        resetForm();
        MultiView1.ActiveViewIndex = 1;
    }
    protected void btDelete_Click(object sender, EventArgs e)
    {
        int EqID = int.Parse(txtID.Text);
        bEquipment.deleteEquipment(EqID);

        DataTable dt = bEquipment.getAllEquipment();
        gvEquipment.DataSource = dt;
        gvEquipment.DataBind();
        MultiView1.ActiveViewIndex = 0;
    }
}
